package com.yzz.composeweatherproject.http.retrofit2;

import android.util.Log;

import androidx.annotation.NonNull;

import com.yzz.composeweatherproject.IDataSuccessListener;
import com.yzz.composeweatherproject.entity.WeatherCityBean;
import com.yzz.composeweatherproject.entity.WeatherProvinceBean;
import com.yzz.composeweatherproject.http.DomainNameUtils;
import com.yzz.composeweatherproject.http.jsoup.JsoupUtils;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class RetrofitHttpUtils {

    private RetrofitHttpUtils() {
    }

    private static class SingltonInstance {
        private static final RetrofitHttpUtils INSTANCE = new RetrofitHttpUtils();
    }

    public static RetrofitHttpUtils getInstacne() {
        return SingltonInstance.INSTANCE;
    }


    public void getShoppingData(String search, IDataSuccessListener<String> listener) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c", "home");
        hashMap.put("v", "b");
        hashMap.put("s", search);
        //获取Retrofit对象
        final Retrofit retrofit = new Retrofit.Builder().baseUrl(DomainNameUtils.smzdmUrl).build();
        retrofit.create(ApiJsoupServer.class).getShoppingData(hashMap).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                try {
                    if (response.body() != null) {
                        final String json = response.body().string();
                        listener.onSuccess(json);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    listener.onFail(e.getMessage());
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
                Log.e("yyzz", "onFailure: " + t.getMessage());
                listener.onFail(t.getMessage());
            }
        });
    }


    public void getWeather(IDataSuccessListener<ArrayList<WeatherProvinceBean>> listener) {
        //获取Retrofit对象

        RxService.createWeatherApi(ApiJsoupServer.class).getWeatherData().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                try {
                    if (response.body() != null) {
                        final String json = response.body().string();
                        listener.onSuccess(JsoupUtils.Companion.getWeatherData(json));
                    } else {
                        listener.onFail("empty");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("yyzz", e.getMessage());
                    listener.onFail(e.getMessage());
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
                Log.e("yyzz", "onFailure: " + t.getMessage());
                listener.onFail(t.getMessage());
            }
        });
    }

    public void getCityWeather(String url, IDataSuccessListener<ArrayList<WeatherCityBean>> listener){

    }



}
